package cn.com.czpb.client.module.video.utils;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import cn.com.czpb.client.module.video.activity.FullScreenPlayVideoActivity;
import cn.com.czpb.client.module.video.view.VideoPlayerView;
import cn.com.czpb.client.utils.SPUtil;

import java.util.List;

/**
 * @author: qiu
 */
public class VideoPlayerHelper {
    private SPUtil spu;
    //被多线程访问的变量
    private static volatile  VideoPlayerHelper sVideoPlayerHelper;//并发编程,同事被多个线程访问,保证每次都读取,现在的值,不被系统优化;
    private VideoPlayerView mVideoPlayerView;
    private ViewGroup mParent;//全屏前或是小窗口前正在播放视频的ListItem
    private ViewGroup mSmallVideoPlayerContainer;//小窗口播放父容器
    private int mCurrPlayPosition = -1;//列表中当前正在播放的视频的位置索引
    private int mLastPlayPosition = -1;//列表中上次播放的视频的位置索引

    private VideoPlayerHelper(Context context) {
        spu=SPUtil.getInstance();
        mVideoPlayerView = new VideoPlayerView(context);
    }

    //double check 唯一 单例
    public static void init(Context context) {
        if(sVideoPlayerHelper == null) {
            synchronized (VideoPlayerHelper.class) {
                if(sVideoPlayerHelper == null) {
                    sVideoPlayerHelper = new VideoPlayerHelper(context);
                }
            }
        }
    }

    public void setSmallVideoPlayerContainer(ViewGroup smallVideoPlayerContainer) {

        if(sVideoPlayerHelper == null) {
            throw new NullPointerException("must be invoke init() method first");
        }
        sVideoPlayerHelper.stop();
        this.mSmallVideoPlayerContainer = smallVideoPlayerContainer;
    }

    public static VideoPlayerHelper getInstance() {

        if(sVideoPlayerHelper == null) {
            throw new NullPointerException("must be invoke init() method first");
        }
        return sVideoPlayerHelper;
    }

    /**
     * 列表播放，可以全屏切换
     * @param parent    加载播放器的根视图
     * @param videoUrl  播放地址
     * @param position  播放器在列表中的位置
     */
    public void play(final ViewGroup parent,final String videoUrl,final int position) {
        boolean isNeedDestroy  = false;//
        //不是停止状态
        if(getVideoPlayState() != VideoPlayState.STOP) {
            mVideoPlayerView.onDestroy();
            isNeedDestroy = true;
    //            mSmallVideoPlayerContainer.setVisibility(View.GONE);
        }
        //正在播放,先销毁原来的,然后, 从新添加播放器
        if (isNeedDestroy){//开新线程
            HandlerTip.getInstance().postDelayed(100, new HandlerTip.HandlerCallback() {
                @Override
                public void postDelayed() {
                    parent.addView(mVideoPlayerView);
                    mVideoPlayerView.play(videoUrl);
                    mCurrPlayPosition = position;
                }
            });
        }else {//直接play;
            Log.i("tag", "play: -->");
            parent.addView(mVideoPlayerView);
            mVideoPlayerView.play(videoUrl);
            mCurrPlayPosition = position;
        }

    }

    /**
     * 全屏播放，可以切换上一个，下一个
     * @param parent   加载播放器的根视图
     * @param videoList 播放地址列表
     * @param position 当前播放第几个视频
     */
    public void play(ViewGroup parent,final List<String>videoList,final int position) {
        if(getVideoPlayState() != VideoPlayState.STOP) {
            mVideoPlayerView.onDestroy();
            //            mSmallVideoPlayerContainer.setVisibility(View.GONE);
        }
        parent.addView(mVideoPlayerView);
        mVideoPlayerView.play(videoList,position);
        mCurrPlayPosition = position;
    }

    /**
     * 切换页面继续播放
     * @param newParent
     * @param videoUrl
     */
    public void playContinue(ViewGroup newParent,String videoUrl){
        ViewGroup oldParent = (ViewGroup)mVideoPlayerView.getParent();
        if (oldParent != null){
            oldParent.removeView(mVideoPlayerView);
        }
        newParent.addView(mVideoPlayerView);
        if (getVideoPlayState() == VideoPlayState.STOP){
            mVideoPlayerView.play(videoUrl);
        }else {
            play();
        }
    }

    public void play() {

        mVideoPlayerView.play();
    }

    public void pause() {

        mVideoPlayerView.pause();
    }

    /**
     * 进入全屏界面
     *
     * @param context
     */
    public void gotoFullScreen(Context context) {

        context.startActivity(new Intent(context, FullScreenPlayVideoActivity.class));
    }

    /**
     * 开始全屏播放
     *
     * @param parent
     * @param exitFullScreenListener
     */
    public void fullScreen(ViewGroup parent, VideoPlayerView.ExitFullScreenListener exitFullScreenListener) {

        mParent = (ViewGroup)mVideoPlayerView.getParent();
        mParent.removeView(mVideoPlayerView);
        mVideoPlayerView.setPlayScreenState(PlayScreenState.FULL_SCREEN);
        mVideoPlayerView.setExitFullScreenListener(exitFullScreenListener);
        parent.addView(mVideoPlayerView);
        mVideoPlayerView.play();
    }

    /**
     * 退出全屏播放
     *
     * @param state
     */
    public void exitFullScreen(VideoPlayState state) {

        mVideoPlayerView.setPlayScreenState(PlayScreenState.NORMAL);
        ViewGroup parent = (ViewGroup) mVideoPlayerView.getParent();
        if(parent != null) {
            parent.removeView(mVideoPlayerView);
        }
        mVideoPlayerView.setExitFullScreenListener(null);
        if (mParent!=null){
            mParent.addView(mVideoPlayerView);
        }
        mParent = null;
        if(state == VideoPlayState.PLAY) {
            mVideoPlayerView.play();
        }
    }

    /**
     * 小窗口播放
     */
    public void smallWindowPlay() {

        mLastPlayPosition = mCurrPlayPosition;
        mCurrPlayPosition = -1;
        mSmallVideoPlayerContainer.setVisibility(View.VISIBLE);
        mParent = (ViewGroup)mVideoPlayerView.getParent();
        mParent.removeView(mVideoPlayerView);
        mVideoPlayerView.setPlayScreenState(PlayScreenState.SMALL);
        mSmallVideoPlayerContainer.addView(mVideoPlayerView, 0);
        if(getVideoPlayState() != VideoPlayState.LOADING) {
            mVideoPlayerView.play();
        }
    }

    /**
     * 小窗口恢复到列表播放
     */
    public void smallWindowToListPlay() {

        mCurrPlayPosition = mLastPlayPosition;
        mLastPlayPosition = -1;
        mSmallVideoPlayerContainer.setVisibility(View.GONE);
        mVideoPlayerView.setPlayScreenState(PlayScreenState.NORMAL);
        mSmallVideoPlayerContainer.removeView(mVideoPlayerView);
        mParent.addView(mVideoPlayerView);
        if(getVideoPlayState() != VideoPlayState.LOADING) {
            mVideoPlayerView.play();
        }
        mParent = null;
    }

    public void stop() {

        if(mVideoPlayerView.getPlayScreenState() == PlayScreenState.SMALL) {
            if(mSmallVideoPlayerContainer != null) {
                mSmallVideoPlayerContainer.setVisibility(View.GONE);
            }
        }
        mVideoPlayerView.onDestroy();
        clear();
    }

    public VideoPlayState getVideoPlayState() {

        return mVideoPlayerView.getVideoPlayState();
    }

    public int getCurrPlayPosition() {

        return mCurrPlayPosition;
    }

    public int getLastPlayPosition() {

        return mLastPlayPosition;
    }

    public void clear() {

        mCurrPlayPosition = -1;
        mLastPlayPosition = -1;
        mParent = null;
    }
}
